package main;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Config {
	private String configFilePath = null;
	private String dbAddress = "jdbc:mysql://localhost/unomobile"; 
	private String dbUserName = "root";
	private String dbPassword = "dan";
	private int portNumber = 1503;
	
	/**
	 * for reading and editing XML Configuration File
	 */
	private Document document = null;
	
	/**
	 * for storing server configuration information 
	 */
	private NodeList serverInfoList = null;
	
	public Config(String filePath) {
		configFilePath = filePath;
	}
	
	private void initConfig() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			document = builder.parse(new File(configFilePath));
			Element root = document.getDocumentElement();
			System.out.println("Root Name: " + root);
			
			serverInfoList = root.getChildNodes();
			
			for(int i = 0; i < serverInfoList.getLength(); i++) {
				Node temp = serverInfoList.item(i);
				if(temp.getNodeType() == Node.ELEMENT_NODE) {
					Element tempE = (Element)temp;
					Node init = tempE.getLastChild();    
				      Node initW = tempE.getFirstChild();    
				      
					if("serverPort".equals(tempE.getNodeName())){
						portNumber = Integer.parseInt(init.getNodeValue());
						System.out.println(initW.getNodeValue());
					}
					else if("dbUserName".equals(tempE.getNodeName())){
						dbUserName = init.getNodeValue();
						System.out.println(initW.getNodeValue());
					}
					else if("dbAddress".equals(tempE.getNodeName())){
						dbAddress = init.getNodeValue();
						System.out.println(initW.getNodeValue());
					}
					else if("dbPassword".equals(tempE.getNodeName())){
						dbPassword = init.getNodeValue();
						System.out.println(initW.getNodeValue());
					}
				}
			}
		}
		catch (ParserConfigurationException e) {
			   e.printStackTrace();    
		}
		catch (SAXException e) {
			e.printStackTrace();    
		}
		catch (IOException e) {
			e.printStackTrace();    
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	
	//load the config information
	public void load() {
		initConfig();
	}
	
	//save the config information
	public void save() {
		
	}
	

	@Override
	public String toString() {
		return "Config [configFilePath=" + configFilePath + ", dbAddress="
				+ dbAddress + ", dbUserName=" + dbUserName + ", dbPassword="
				+ dbPassword + ", portNumber=" + portNumber + ", document="
				+ document + ", serverInfoList=" + serverInfoList + "]";
	}

	public String getConfigFilePath() {
		return configFilePath;
	}

	public void setConfigFilePath(String configFilePath) {
		this.configFilePath = configFilePath;
	}

	public String getDbAddress() {
		return dbAddress;
	}

	public void setDbAddress(String dbAddress) {
		this.dbAddress = dbAddress;
	}

	public String getDbUserName() {
		return dbUserName;
	}

	public void setDbUserName(String dbUserName) {
		this.dbUserName = dbUserName;
	}
	
	

	public String getDbPassword() {
		return dbPassword;
	}

	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}

	public int getPortNumber() {
		return portNumber;
	}

	public void setPortNumber(int portNumber) {
		this.portNumber = portNumber;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public NodeList getServerInfoList() {
		return serverInfoList;
	}

	public void setServerInfoList(NodeList serverInfoList) {
		this.serverInfoList = serverInfoList;
	}
	
}
